package com.zhitongyun.devlop.ui;

import android.app.Fragment;
import android.app.FragmentManager;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.joanzapata.android.BaseAdapterHelper;
import com.joanzapata.android.QuickAdapter;
import com.zhitongyun.devlop.R;
import com.zhitongyun.devlop.db.bean.AppCategoryEntity;
import com.zhitongyun.devlop.net.HaleyPluginRequest;
import com.zhitongyun.devlop.net.RequestCallBack;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by huanglei on 6/3/16.
 */
public class HomeActivity extends AppCompatActivity implements ListView.OnItemClickListener {

    @Bind(R.id.drawer_layout)
    DrawerLayout mDrawerLayout;
    @Bind(R.id.left_drawer)
    ListView mDrawerList;
    @Bind(R.id.tool_bar)
    Toolbar toolbar;
    private ActionBarDrawerToggle mDrawerToggle;

    private QuickAdapter adapter;

    private String[] appCategorys = new String[]{"分类1", "分类2", "分类3", "分类4", "分类5"};
    private List<AppCategoryEntity> allCategorys = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        ButterKnife.bind(this);

        initData();

        //add ActionBarDrawerToggle
        mDrawerToggle = new ActionBarDrawerToggle(
                this,
                mDrawerLayout,
                toolbar,
                R.string.drawer_open,
                R.string.drawer_close) {
            public void onDrawerClosed(View view) {
                invalidateOptionsMenu();
            }

            public void onDrawerOpened(View drawerView) {
                invalidateOptionsMenu();
            }
        };
        mDrawerLayout.addDrawerListener(mDrawerToggle);

        if (savedInstanceState == null) {
            selectItem(0);
        }
    }

    private void initData() {
        //先获取本地数据再从服务器拿

        HaleyPluginRequest.getAppCategory("", new RequestCallBack<List<AppCategoryEntity>>() {
            @Override
            public void back(boolean success, List<AppCategoryEntity> entities) {
                if (success) {
                    drawerAdapter(entities);
                }
            }
        });

    }

    private void drawerAdapter(List<AppCategoryEntity> allCategorys) {

        AppCategoryEntity entity = new AppCategoryEntity();
        entity.setAppCategoryName("推荐");
        allCategorys.add(0, entity);

        if (adapter == null) {
            adapter = new QuickAdapter<AppCategoryEntity>(this,
                    R.layout.layout_item_show_category_lay,
                    allCategorys) {
                @Override
                protected void convert(BaseAdapterHelper helper, AppCategoryEntity entity) {
                    helper.setText(R.id.category_name, entity.getAppCategoryName());
                }
            };
            mDrawerList.setAdapter(adapter);
            mDrawerList.setOnItemClickListener(this);
        } else {
            adapter.replaceAll(allCategorys);
        }

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        selectItem(position);
    }

    public void selectItem(int position) {
        Fragment fragment = new AppListFragment();

        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

        mDrawerList.setItemChecked(position, true);
        mDrawerLayout.closeDrawer(mDrawerList);
    }

    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        mDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mDrawerToggle.onConfigurationChanged(newConfig);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }
}
